/**
 * 一个线程做累加操作，另一个在累加结束后打印
 */
public class NCount {
    public static void main(String[] args) throws InterruptedException {
       Result result = new Result();
       Thread thread1 = new Thread(() -> {
           int sum = 0;
           for (int i = 1; i <= 10000; i++) {
               sum +=i;
           }
           result.Sum = sum;
           //进行赋值操作完成后，唤醒打印
           synchronized (result.lock) {
               result.lock.notifyAll();
           }
       });
       thread1.start();
       //thread1.join();//一种等待方式
        synchronized (result.lock) {
            while(result.Sum == 0) {
                result.lock.wait();
            }
            System.out.println(result.Sum);
        }
    }
}
class Result {
    public int Sum = 0;
    public Object lock = new Object();
}